﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CodeGame
{
    class ContentManager
    {
        private SpriteBatch Batch;

        private Dictionary<string, Texture2D> Textures = new Dictionary<string, Texture2D>();
        private Dictionary<string, SpriteFont> SpriteFonts = new Dictionary<string, SpriteFont>();

        private Microsoft.Xna.Framework.Content.ContentManager Manager;

        private Texture2D Pixel;
        private Texture2D ErrorTexture;

        public ContentManager(MainGame game)
        {
            this.Manager = game.Content;
            this.Batch = game.Batch;

            this.Pixel = new Texture2D(Singletons.Device, 1, 1);
            this.Pixel.SetData<Color>(new Color[] { Color.White });

            this.ErrorTexture = this.Manager.Load<Texture2D>("error");
        }

        public Texture2D GetTexture(string filename)
        {
            if (this.Textures.ContainsKey(filename))
            {
                return this.Textures[filename];
            }
            else
            {
                if (File.Exists(this.Manager.RootDirectory + "\\" + filename + ".xnb"))
                {
                    this.Textures.Add(filename, this.Manager.Load<Texture2D>(filename));
                    return this.Textures[filename];
                }
                else
                {
                    Console.WriteLine("Content was not found at: " + this.Manager.RootDirectory + "\\" + filename + ".xmb");
                    return this.ErrorTexture;
                }
            }
        }

        public SpriteFont GetFont(string fontName)
        {
            if (this.SpriteFonts.ContainsKey(fontName))
            {
                return this.SpriteFonts[fontName];
            }
            else
            {
                this.SpriteFonts.Add(fontName, this.Manager.Load<SpriteFont>(fontName));
                return this.SpriteFonts[fontName];
            }
        }

        public void DrawText(string font, string text, int x, int y, Color col)
        {
            this.Batch.DrawString(this.GetFont(font), text, new Vector2(x, y), col);
        }

        public void DrawImage(string texture, int x, int y, Color tint)
        {
            Texture2D text = this.GetTexture(texture);
            this.Batch.Draw(text, new Rectangle(x, y, text.Width, text.Height), tint);
        }

        public void DrawImage(string texture, int x, int y, float angle, float scale, Color tint)
        {
            Texture2D text = this.GetTexture(texture);
            this.Batch.Draw(text, new Rectangle((int)x, (int)y, (int)((float)text.Width * scale), (int)((float)text.Height * scale)),
                null, tint, angle, new Vector2(text.Width / 2, text.Height / 2), SpriteEffects.None, 0);
        }

        public void DrawImage(Texture2D texture, int x, int y, Color tint)
        {
            this.Batch.Draw(texture, new Rectangle(x, y, texture.Width, texture.Height), tint);
        }

        public void DrawRectangle(Rectangle rect, Color tint)
        {
            this.Batch.Draw(this.Pixel, rect, tint);
        }
    }
}
